﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace 视频流媒体管理.Code
{
    public class HttpPost
    {

        public string PostData(string url, string ContentType, string postData, Dictionary<string, string> header)
        {
            byte[] postdatabyte = Encoding.UTF8.GetBytes(postData);
            string str = this.PostData(url,  ContentType, postdatabyte, header);
            return str;
        }

        public string PostData(string url, string ContentType, byte[] postData, Dictionary<string, string> header)
        {

            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webrequest.Method = "POST"; // "POST";
            webrequest.ContentType = ContentType; // "multipart/form-data; boundary=--------------------------786090941028272689697035";
            if (webrequest.ContentType == "")
                webrequest.ContentType = "application/json";
            webrequest.Proxy = null;
            foreach (var item in header)
            {
                webrequest.Headers.Add(item.Key, item.Value);
            }

            webrequest.ContentLength = postData.Length;
            using (Stream stream = webrequest.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length);

                using (var httpWebResponse = webrequest.GetResponse())
                {
                    using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
                    {
                        string ret = responseStream.ReadToEnd();
                        return ret;
                    }
                }
            }
        }

        public string GetData(string url, string ContentType, Dictionary<string, string> header)
        {
            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webrequest.Method = "GET"; // "POST";
            webrequest.ContentType = ContentType; 
            if (webrequest.ContentType == "")
                webrequest.ContentType = "text/html";
            webrequest.Proxy = null;
            foreach (var item in header)
            {
                webrequest.Headers.Add(item.Key, item.Value);
            }

            using (var httpWebResponse = webrequest.GetResponse())
            {
                using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    string ret = responseStream.ReadToEnd();
                    return ret;
                }
            }
        }

        public string 组合FormData(Dictionary<string, string> formData, out string ContentType)
        {
            //string Boundary = "--WebKitFormBoundary39B5a5e2FWoGbphs";
            string Boundary = "--------------------------963017638438506631499954";
            //构造POST请求体
            ContentType = "multipart/form-data;boundary=" + Boundary;            //https://blog.csdn.net/qq_51860400/article/details/132455026


            StringBuilder sb = new StringBuilder();
            //组成普通参数信息
            foreach (var item in formData)
            {
                sb.AppendLine("--" + Boundary);
                sb.AppendLine("Content-Disposition:form-data;name=\"" + item.Key + "\"");
                sb.AppendLine();
                sb.AppendLine(item.Value);
            }
            sb.AppendLine("--" + Boundary);
            return sb.ToString();
        }
    }
}